package com.example.exam.controller;

import com.example.exam.entity.Admin;
import com.example.exam.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * (Admin)表控制层
 *
 * @author makejava
 * @since 2021-09-04 15:02:44
 */
@RestController
@RequestMapping("admin")
public class AdminController {
    /**
     * 服务对象
     */
    @Autowired
    private AdminService adminService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Admin selectOne(Integer id) {
        return this.adminService.queryById(id);
    }

    @RequestMapping("/insert")
    public int insert(Admin admin) {
        return adminService.insert2(admin);
    }

    @RequestMapping("/update")
    public boolean update(Admin admin, HttpSession session) {
        Integer userId = (Integer) session.getAttribute("userId");
        System.out.println(admin.getAdminName());
        System.out.println(admin.getEmail());
        System.out.println(admin.getTel());
        Admin admin1 = adminService.queryById(userId);
        admin1.setTel(admin.getTel());
        admin1.setAdminname(admin.getAdminName());
        admin1.setEmail(admin.getEmail());
        return adminService.update2(admin1);
    }

    @RequestMapping("/select")
    public List<Admin> select(int id) {
        return adminService.select2(id);
    }
}